
#ifndef PIXMAPAREA_H
#define PIXMAPAREA_H

#include <BSet>
#include <BMap>
#include <BTable>
#include <BPixmap>
#include <BImage>
#include <BPolygon>
#include <BMenu>
#include <BScrollArea>
#include <BEventFilter>

#include "ImageLayer.h"

class PixmapArea : public BScrollArea
{
	BOBJECT(PixmapArea);
public:
	PixmapArea(BWidget* parent = 0);
	~PixmapArea();

	BPixmap* pixmap() const;
	BImage* image() const;

	ImageLayer* findLayer(const BImage* image) const;

	void enableEvent();
	void disableEvent();
	bool eventEnabled();

	void setZoom(int zoom);
	int zoom() const;

	void setAreaImage(BImage* areaImage);
	BImage* areaImage() const;

	void setGridDrakness(BByte darkness);
	BByte gridDarkness() const;

	void select(BWidget* widget);
	void unselect(BWidget* widget);
	void clearSelection();
	BWidgetSet& selectedWidgets();
	const BWidgetSet& selectedWidgets() const;

	BRect mapToPixmap(const BRect& rect) const;
	BRect mapFromPixmap(const BRect& rect) const;

	BPoint mapToPixmap(const BPoint& rect) const;
	BPoint mapFromPixmap(const BPoint& rect) const;

protected:
	void adjustArea();
	void freshCanvas();
	BString findValidName(const BString& name);

protected:
	void slotPixmapResized();
	void slotPixmapDirty();
	void slotPixmapFramed();

protected:
	virtual void scrolling(Orientation orientation);

protected:
	virtual void event(const BEvent& event);

	virtual void resizeEvent(const BEvent& event);

	virtual void freshEvent(const BEvent& event);
	virtual void paintEvent(const BEvent& event);

	virtual void keyPressEvent(const BKeyEvent& event);

	virtual void mouseEnterEvent(const BMouseEvent& event);
	virtual void mouseLeaveEvent(const BMouseEvent& event);
	virtual void mousePressEvent(const BMouseEvent& event);
	virtual void mouseReleaseEvent(const BMouseEvent& event);

	virtual void mouseMoveEvent(const BMouseEvent& event);
	virtual void mouseWheelEvent(const BMouseEvent& event);

	virtual void mouseClickEvent(const BMouseEvent& mouseEvent);
	virtual void mouseDoubleClickEvent(const BMouseEvent& mouseEvent);

private:
	bool				_eventEnabled;
	BByte				_darkness;
	BImageHolder		_canvas;
	BImageHolder		_areaImage;
	BPixmapHolder		_pixmap;
	BImageHolder		_image;
	int					_zoom;
	BRect				_area;
	BArray<BPolygon>	_polygons;
	BWidget*			_hoveredLayer;
	bool				_draging;
	BPoint				_dragPoint;
	BRect				_dragArea;
	BWidgetSet			_selectedWidgets;
};

#endif
